<template>
  <div class="container">
    <!-- 左侧：图片上传与分类 -->
    <div class="left-panel">
      <div class="picture-upload">
        <a-upload
          list-type="picture-card"
          :show-upload-list="false"
          :custom-request="handleUpload"
          :before-upload="beforeUpload"
        >
          <img v-if="picture?.url" :src="picture?.url" alt="avatar" />
          <div v-else>
            <loading-outlined v-if="loading"></loading-outlined>
            <plus-outlined v-else></plus-outlined>
            <div class="ant-upload-text">点击或拖拽上传图片</div>
          </div>
        </a-upload>
        <a-button type="primary" @click="submit" style="width: 100%; margin-top: 16px;">
          提交
        </a-button>

        <div v-if="showresult">
          <a-card title="图片分类结果" style="margin-top: 16px;">
            <a-card-meta>
              <template #title>
                <a-typography-text v-if="!classifying" strong>{{ className }}</a-typography-text>
                <a-spin v-else />
              </template>

            </a-card-meta>
          </a-card>
          <a-button type="default" @click="startChat" style="width: 100%; margin-top: 16px;">
            问一问{{ className }}的相关知识
          </a-button>
        </div>
      </div>
    </div>

    <!-- 右侧：聊天室 -->
    <div class="right-panel">

      <ChatRoom v-if="chatVisible" :animal-name="className" />
    </div>
  </div>
</template>


<script setup lang="ts">
import { PlusOutlined, LoadingOutlined } from '@ant-design/icons-vue';
import { message, type UploadProps } from 'ant-design-vue'
import { ref } from 'vue'
import {
  uploadPictureReturnUrlUsingPost,
} from '@/api/fileController.ts'
import PictureVO = API.PictureVO
import { classifyImageUsingPost } from '@/api/classifybirdController.ts'
import ChatRoom from '@/components/ChatRoom.vue'
const className = ref<string>()
const beforeUpload = (file: UploadProps['fileList'][number]) => {
  const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
  if (!isJpgOrPng) {
    message.error('不支持上传该格式的图片，推荐 jpg 或 png')
  }
  const isLt2M = file.size / 1024 / 1024 < 2
  if (!isLt2M) {
    message.error('不能上传超过 2M 的图片')
  }
  return isJpgOrPng && isLt2M
}
const loading = ref<boolean>(false)
const picture = ref<PictureVO>()
const showresult = ref<boolean>(false)
const classifying = ref<boolean>(false)
/**
 * 上传
 * @param file
 */
const handleUpload = async ({ file }: any) => {
  loading.value = true
  try {
    const res = await uploadPictureReturnUrlUsingPost({},file)
    if (res.data.code === 0 && res.data.data) {
      picture.value = res.data.data
      message.success('图片上传成功')
    } else {
      message.error('图片上传失败，' + res.data.message)
    }
  } catch (error) {
    console.error('上传图片时发生错误:', error)
    message.error('图片上传失败')
  } finally {
    loading.value = false

  }
}
const submit =async () => {
  if (!picture?.value?.url) {
    message.error('请先上传图片')
    return
  }
  classifying.value = true
  try {
    const res = await classifyImageUsingPost({ imageUrl: picture?.value?.url })
    if (res.data.code === 0 && res.data.data) {
      className.value = res.data.data
      showresult.value = true
      message.success('图片分类成功')
    } else {
      message.error('图片分类失败，' + res.data.message)
    }
  } catch (error) {
    console.error('图片分类时发生错误:', error)
    message.error('图片分类失败')
  } finally {
    classifying.value = false
  }
}
const chatVisible = ref<boolean>(false)

const startChat = () => {
  if (!className.value) {
    message.error('没有可用的分类结果')
    return
  }
  chatVisible.value = true
}

</script>
<style scoped>.container {
  display: flex;
  width: 100%;
  height: 80vh;
  overflow: hidden;
}

.left-panel {
  flex: 1;
  padding: 20px;
  background-color: #fff;
  border-right: 1px solid #e8e8e8;
  overflow-y: auto;
}

.right-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background-color: #fff;
}

.picture-upload :deep(.ant-upload) {
  width: 100% !important;
  height: 100% !important;
  min-height: 152px;
  min-width: 152px;
}

.picture-upload img {
  max-width: 100%;
  max-height: 480px;
}

.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>
